/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Clases;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author JULIAN
 */
public class CosultaFacturaGerente extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String factura=request.getParameter("numero de factura");
            SQL_Consultas obj = new SQL_Consultas();
            obj.setFactura(factura);

            PreparedStatement st = obj.GetDatosVenta();
            ResultSet resultado = st.executeQuery();

            if(!resultado.next()){
                 response.sendRedirect("OpcionesGerente.jsp");
                                 }
            else{
            HttpSession session = request.getSession(true);
            session.setAttribute("numero_factura", resultado.getString("Numero_de_Factura"));
            session.setAttribute("cod_ven", resultado.getString("Codigo_del_Vendedor"));
            session.setAttribute("nom_ven", resultado.getString("Nombre_Vendedor"));
            session.setAttribute("tpago", resultado.getString("Tipo_de_Pago"));
            session.setAttribute("fecha", resultado.getString("Fecha"));
            session.setAttribute("cod_caja", resultado.getString("Codigo_de_Caja"));
            session.setAttribute("cod_almacen", resultado.getString("Codigo_de_Almacen"));
            session.setAttribute("id_cliente", resultado.getString("Documento_Cliente"));
            session.setAttribute("nom_cliente", resultado.getString("Nombre_del_Cliente"));
            session.setAttribute("iva", resultado.getString("IVA"));
            session.setAttribute("costo", resultado.getString("Costo"));
            st.close();
            resultado.close();
            response.sendRedirect("MuestraFactura.jsp");
            }
        } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(CosultaFacturaGerente.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(CosultaFacturaGerente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
